package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import me.shurufa.R;
import me.shurufa.activities.BookListActivity;
import me.shurufa.activities.UserListActivity;
import me.shurufa.adapter.DiscoverUserAdapter;
import me.shurufa.utils.Constants;
import me.shurufa.utils.LogUtils;

/**
 * 发现页面
 *
 * @FileName: me.shurufa.activities.DiscoverFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-18 10:52
 */
public class DiscoverFragment extends BaseFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.more_books)
    TextView moreBooks;
    @Bind(R.id.more_authors)
    TextView moreAuthors;
    @Bind(R.id.left_text)
    TextView leftText;
    @Bind(R.id.right_image)
    ImageView rightImage;
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;

    private DiscoverBookFragment bookFragment;
    private DiscoverUserFragment userFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        setRefresh();
        moreAuthors.setOnClickListener(this);
        moreBooks.setOnClickListener(this);
        backInToolbar.setVisibility(View.GONE);
        titleTextView.setText(getString(R.string.discover));
        initList();
    }

    private void setRefresh() {
        ptrRefresh.setResistance(4.2F);
        ptrRefresh.setRatioOfHeaderHeightToRefresh(1.0F);
        ptrRefresh.setDurationToClose(300);
        ptrRefresh.setDurationToCloseHeader(1000);
        ptrRefresh.setLoadingMinTime(500);
        ptrRefresh.setPullToRefresh(false);
        ptrRefresh.setKeepHeaderWhenRefresh(true);
        ptrRefresh.disableWhenHorizontalMove(true);
        ptrRefresh.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout ptrFrameLayout, View view, View view1) {
                boolean canbePullDown = PtrDefaultHandler.checkContentCanBePulledDown(ptrFrameLayout, view, view1);
                return canbePullDown;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                refresh();
            }
        });
    }

    /**
     * 加载两个Fragment 至DiscoverFragment中
     */
    private void initList() {
        bookFragment = new DiscoverBookFragment();
        userFragment = new DiscoverUserFragment();
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.book_container, bookFragment);
        transaction.add(R.id.user_container, userFragment);
        transaction.commit();
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.more_authors:
                intent = new Intent(getActivity(), UserListActivity.class);
                startActivity(intent);
                break;
            case R.id.more_books:
                intent = new Intent(getActivity(), BookListActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void refresh() {
        super.refresh();
        ptrRefresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ptrRefresh.isRefreshing()) {
                    ptrRefresh.refreshComplete();
                }
            }
        },2000);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        if (bookFragment != null) {
            bookFragment.refresh();
        }
        if (userFragment != null) {
            userFragment.refresh();
        }
    }

    int completeCount = 0;
    public void onEventMainThread(Intent intent) {
        if (intent.getAction().equals(Constants.ACTION_DISCOVER_REFRESH_COMPLETE)){
           completeCount++;
            LogUtils.kLog().e("count "+completeCount);
           if (completeCount%2==1&&(bookFragment==null||userFragment==null)){
               if (ptrRefresh.isRefreshing()){
                   ptrRefresh.refreshComplete();
                   completeCount =0;
               }
           }else if(completeCount%2==0){
               if(ptrRefresh.isRefreshing()){
                   ptrRefresh.refreshComplete();
                   completeCount=0;
               }
           }
        }
    }
}
